package br.ufpb.sistcomp.persistence;

import java.util.ArrayList;
import java.util.List;

import br.ufpb.sistcomp.database.InformationDatabase;
import br.ufpb.sistcomp.entity.Teacher;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Class that handles the persistence of Teacher.
 * 
 * @author Renan Oliveira, www.renanoliveira.com
 * @author Diego Sousa, www.diegosousa.com
 * @version 1.0
 * @since 01/01/2012
 * 
 *        Copyright (C) 2012 SistComp Empresa Jr.
 */

public class TeacherPersistence {

	private static TeacherPersistence instance;
	private static SQLiteDatabase sqliteDatabase;
	private ContentValues values;

	private TeacherPersistence() {
		values = new ContentValues();
	}

	public static synchronized TeacherPersistence getInstance(Context context) {

		if (instance == null) {
			instance = new TeacherPersistence();
			sqliteDatabase = context.openOrCreateDatabase(
					InformationDatabase.NAME_DATABASE, Context.MODE_PRIVATE,
					null);
		}
		return instance;
	}

	public void insertTeacher(Teacher teacher) {

		values.put(InformationDatabase.ATRIBUTE_TEACHERS_NAME,
				teacher.getName());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_MAIL,
				teacher.getMail());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_PHONE,
				teacher.getPhone());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_CELL,
				teacher.getCell());

		sqliteDatabase.insert(InformationDatabase.GETNAME_TABLE_TEACHERS, null,
				values);
	}

	public void removeTeacher(String nameTeacher) {

		sqliteDatabase.delete(InformationDatabase.GETNAME_TABLE_TEACHERS,
				InformationDatabase.ATRIBUTE_TEACHERS_NAME + "=" + "'" +nameTeacher+ "'",
				null);

	}

	public void updateTeacher(Teacher teacher) {						

		String where = InformationDatabase.ATRIBUTE_TEACHERS_ID_TEACHER + "="
				+ teacher.getId();
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_NAME,
				teacher.getName());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_MAIL,
				teacher.getMail());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_PHONE,
				teacher.getPhone());
		values.put(InformationDatabase.ATRIBUTE_TEACHERS_CELL,
				teacher.getCell());

		sqliteDatabase.update(InformationDatabase.GETNAME_TABLE_TEACHERS,
				values, where, null);

	}

	public Teacher locateTeacher(String nameTeacher) {
		Teacher teacher = null;

		Cursor cursorTeacher = sqliteDatabase.query(
				InformationDatabase.GETNAME_TABLE_TEACHERS, new String[] {
						InformationDatabase.ATRIBUTE_TEACHERS_ID_TEACHER,
						InformationDatabase.ATRIBUTE_TEACHERS_NAME,
						InformationDatabase.ATRIBUTE_TEACHERS_MAIL,
						InformationDatabase.ATRIBUTE_TEACHERS_PHONE,
						InformationDatabase.ATRIBUTE_TEACHERS_CELL }, null,
				null, null, null, null);

		// course is before the last line, the cursor moves to next line.
		if (cursorTeacher.getPosition() == -1) {
			cursorTeacher.moveToFirst();
		}

		if (cursorTeacher != null) {
			while (!cursorTeacher.isAfterLast()) {// repeats until the last
													// line.

				if (nameTeacher
						.equals(cursorTeacher
								.getString(
										cursorTeacher
												.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_NAME))
								.toString())) {
					teacher = new Teacher();

					teacher.setId(cursorTeacher.getInt(cursorTeacher
							.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_ID_TEACHER)));
					teacher.setName(cursorTeacher.getString(cursorTeacher
							.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_NAME)));
					teacher.setMail(cursorTeacher.getString(cursorTeacher
							.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_MAIL)));
					teacher.setCell(cursorTeacher.getString(cursorTeacher
							.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_CELL)));
					teacher.setPhone(cursorTeacher.getString(cursorTeacher
							.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_PHONE)));
					return teacher;
				} else {
					cursorTeacher.moveToNext();
				}
			}// end while.
		}
		return null;
	}

	public List<Teacher> listAllTeachers() {
		Teacher teacher = null;
		ArrayList<Teacher> listTeachers = new ArrayList<Teacher>();

		Cursor cursorTeacher = sqliteDatabase.query(
				InformationDatabase.GETNAME_TABLE_TEACHERS, new String[] {
						InformationDatabase.ATRIBUTE_TEACHERS_ID_TEACHER,
						InformationDatabase.ATRIBUTE_TEACHERS_NAME,
						InformationDatabase.ATRIBUTE_TEACHERS_MAIL,
						InformationDatabase.ATRIBUTE_TEACHERS_PHONE,
						InformationDatabase.ATRIBUTE_TEACHERS_CELL }, null,
				null, null, null, null);

		// course is before the last line, the cursor moves to next line.
		if (cursorTeacher.getPosition() == -1) {
			cursorTeacher.moveToFirst();
		}

		if (cursorTeacher != null) {
			while (!cursorTeacher.isAfterLast()) {// repeats until the last
													// line.

				teacher = new Teacher();

				teacher.setId(cursorTeacher.getInt(cursorTeacher
						.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_ID_TEACHER)));
				teacher.setName(cursorTeacher.getString(cursorTeacher
						.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_NAME)));
				teacher.setMail(cursorTeacher.getString(cursorTeacher
						.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_MAIL)));
				teacher.setCell(cursorTeacher.getString(cursorTeacher
						.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_CELL)));
				teacher.setPhone(cursorTeacher.getString(cursorTeacher
						.getColumnIndex(InformationDatabase.ATRIBUTE_TEACHERS_PHONE)));
				listTeachers.add(teacher);
				cursorTeacher.moveToNext();
			}
		}
		return listTeachers;
	}
}
